<?php
/**
 * 购物车Action
 * @author tangbl
 *
 */
class CartAction extends CommonAction {
	
	/**
	 * 用户购物车
	 */
	public function index(){
		
		$map['tableid'] = $this->_req("tableid", "trim", "");
		$map['dataid']  = $this->_req("dataid" , "trim", "");
		$map['company'] = $this->_req("company", "intval",0);
		
		$model = D("Cart");
		$list  = $model->listCart($map);
		
		$this->success($list);
	}
	
	/**
	 * 转移购物车
	 * @param  array  $map     应用参数(company,outletid,addid,addno)
	 * @param  String $tableid 旧用户模型
	 * @param  String $dataid  旧用户编号
	 * 
	 * @param  String $old     新用户模型
	 * @param  String $oldid   新用户编号
	 */
	public function transfer(){
		
		$map['tableid'] = $this->_req("tableid", "trim", "");
		$map['dataid']  = $this->_req("dataid" , "trim", "");
		$map['company'] = $this->_req("company", "intval",0);
		
		$con['tableid'] = $this->_req("old"    , "trim", "");
		$con['dataid']  = $this->_req("oldid"  , "trim", "");
		
		$model = D("Cart");
		$id    = $model->transfer($map, $con);
		if($id){
			$this->success($id);
		}else{
			$this->error($id);
		}
	}
	
	/**
	 * 添加商品到购物车
	 * @param  array  $map     应用参数(company,outletid,addid,addno)
	 * @param  String $tableid 用户模型
	 * @param  String $dataid  用户编号
	 * @param  int    $pid     商品id
	 * @param  int    $num     商品数量
	 */
	public function add(){
		
		$map = parent::getParams();
		
		$map['tableid'] = $this->_req("tableid", "trim", "");
		$map['dataid']  = $this->_req("dataid" , "trim", "");
		$map['pid']     = $this->_req("pid"    , "intval",0);
		$map['num']     = $this->_req("num"    , "intval",1);
		$map['size']    = $this->_req("size"   , "intval",0);
		$map['unit']    = $this->_req("unit"   , "trim" ,"");
		
		$model = D("Cart");
		$id    = $model->addCart($map);
		if($id){
			$this->success($id);
		}else{
			$this->error($id);
		}
	}
	
	/**
	 * 删除某项商品
	 */
	public function del(){
		
		$map['tableid'] = $this->_req("tableid", "trim", "");
		$map['dataid']  = $this->_req("dataid" , "trim", "");
		$map['company'] = $this->_req("company", "intval",0);
		$map['id']      = $this->_req("id"     , "intval",0);
		
		$model = D("Cart");
		$id    = $model->delCart($map);
		if($id){
			$this->success($id);
		}else{
			$this->error($id);
		}
	}
	
	/**
	 * 更改某项商品的数量
	 */
	public function num(){
		
		$map['tableid'] = $this->_req("tableid", "trim", "");
		$map['dataid']  = $this->_req("dataid" , "trim", "");
		$map['company'] = $this->_req("company", "intval",0);
		$map['id']      = $this->_req("id"     , "intval",0);
		
		$num = $this->_req("num", "intval",1);
		
		$model = D("Cart");
		$id    = $model->num($map,$num);
		if($id){
			$this->success($id);
		}else{
			$this->error($id);
		}
	}
	
	/**
	 * 清空购物车
	 */
	public function remove(){
		
		$map['tableid'] = $this->_req("tableid", "trim", "");
		$map['dataid']  = $this->_req("dataid" , "trim", "");
		$map['company'] = $this->_req("company", "intval",0);
		
		$model = D("Cart");
		$id    = $model->remove($map);
		if($id){
			$this->success($id);
		}else{
			$this->error($id);
		}
	}

}
?>